﻿$(document).ready(function () {
    $("#ddlProvince").change(function () { GetCity('', '','','') });
    $("#ddlCity").change(function () { GetDistrict('', '','') });
    $("#ddlDistrict").change(function () { GetSchool('', '') });
});

function GetByJquery(province, city, _county, school) {
    $("#ddlProvince").empty(); //清空省份SELECT控件 
    $("<option></option>").val("").text("请选择").appendTo($("#ddlProvince"));
    $("#ddlCity").empty(); //清空城市SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlCity"));
    $("#ddlDistrict").empty(); //清空市区SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlDistrict"));
    $("#ddlSchool").empty(); //清空学校SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlSchool"));
 
    $.getJSON("/user/controls/service.ashx?pid=0&now=" + new Date(), function (data) {
        if (data != "no") {
            $.each(data, function (i, item) {
                $("<option></option>")
                    .val(item["id"])
                    .text(item["area"])
                    .appendTo($("#ddlProvince"));
            });
            $("#ddlProvince").children("option[value='" + province + "']").attr("selected", "selected");
           
            if (province == '')
                GetCity(province, city, _county,school);
        }
    });

}

function GetCity(province, city, _county,school) {
    $("#ddlCity").empty(); //清空城市SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlCity"));
    $("#ddlDistrict").empty(); //清空市区SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlDistrict"));
    $("#ddlSchool").empty(); //清空学校SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlSchool"));
    var url = "";
  
    if (province != "")
        url = "/user/controls/service.ashx?pid=" + province+"&now=" + new Date();
    else
        url = "/user/controls/service.ashx?pid=" + $("#ddlProvince").val() + "&now=" + new Date();
   // alert(url);
    $.getJSON(encodeURI(url), function (data) {
            if (data != null) {
                $.each(data, function (i, item) {
                    $("<option></option>")
                          .val(item["id"])
                        .text(item["area"])
                        .appendTo($("#ddlCity"));
                });
                $("#ddlCity").children("option[value='" + city + "']").attr("selected", "selected");
                if (city == '')
                    GetDistrict(city, _county,school);
            }
    });
}

function GetDistrict(city, _county,school) {
    $("#ddlDistrict").empty(); //清空市区SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlDistrict"));
    $("#ddlSchool").empty(); //清空学校SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlSchool"));
    var url = "";
    if (city != "")
        url = "/user/controls/service.ashx?pid=" + city + "&now=" + new Date();
    else
        url = "/user/controls/service.ashx?pid=" + $("#ddlCity").val() + "&now=" + new Date();
    $.getJSON(encodeURI(url), function (data) {
        if (data != null) {
            $.each(data, function (i, item) {
                $("<option></option>")
                      .val(item["id"])
                    .text(item["area"])
                    .appendTo($("#ddlDistrict"));
            });
            $("#ddlDistrict").children("option[value='" + _county + "']").attr("selected", "selected");
           
            if (_county == '')
                GetSchool(_county, school);
        }
    });
}

function GetSchool(_county, school) {
    
    $("#ddlSchool").empty(); //清空学校SELECT控件
    $("<option></option>").val("").text("请选择").appendTo($("#ddlSchool"));
    var url = "";
    if (_county != "")
        url = "/user/controls/service.ashx?areaid=" + _county + "&now=" + new Date();
    else
        url = "/user/controls/service.ashx?areaid=" + $("#ddlDistrict").val() + "&now=" + new Date();
    
    $.getJSON(encodeURI(url), function (data) {
        if (data != null) {
            $.each(data, function (i, item) {
                $("<option></option>")
                      .val(item["id"])
                    .text(item["shcoolname"])
                    .appendTo($("#ddlSchool"));
            });
            $("#ddlSchool").children("option[value='" + school + "']").attr("selected", "selected");
            
        }
    });
}

